'''
@Company: TWL
@Author: xue jian
@Email: xuejian@kanzhun.com
@Date: 2020-04-09 19:13:17
'''
# -*- coding: UTF-8 -*-

from base_train import BaseTrain
import numpy as np

import xgboost as xgb

preds_out = open("xgb_preds_out_t", 'w')

class XgboostTrain(BaseTrain):
    def __init__(self, file_path, dates, watch_num, batch_size, thread_num, read_data_parallel, features, m_path):
        # super(XgboostTrain, self).__init__(file_path, dates, fea_index, watch_num, batch_size, thread_num)
        BaseTrain.__init__(self, file_path, dates, watch_num, batch_size, thread_num, read_data_parallel)
        self.features = features
        self.bst = xgb.Booster(model_file=m_path)

    def batch_train(self, batch_data, batch_label):
        tmp_data = []
        for fea in self.features:
            #print(fea)
            tmp_data.append(batch_data[:, self.fea_index[fea]].astype(np.float))

        train_data = np.asarray(tmp_data).T
        batch_train = xgb.DMatrix(train_data, batch_label)

        preds = self.bst.predict(batch_train)
        sess = batch_data[:, self.fea_index['sessionid']]
        jobid = batch_data[:, self.fea_index['job_id']]
        expid = batch_data[:, self.fea_index['exp_id']]
        for i in range(len(sess)):
            key = sess[i] + '\t' + jobid[i] + '\t' + expid[i]
            value = preds[i]
            preds_out.write(key + '\t' + str(value) + '\t' + str(batch_label[i]) + '\n')
        return preds, 0


if __name__ == '__main__':
    m_path = "/home/desktop/2019-06-04/gbdt.bin"
    features = ['b2g_static_match', 'boss_jh_el', 'b2g_major_w2v_gof', 'exp_pas_addf_num_1d3', 'b2g_cmp_level_gof',
                'b2g_salary_match', 'b2g_degree_match', 'exp_pas_addf_num_24h', 'b2g_workyears_match', 'boss_eh_jl',
                'job_f1_addf_rate_1d3', 'exp_resp_num_1d3', 'b2g_als_gof', 'b2g_salary_rel_match',
                'b2g_workyears_rel_match', 'geek_chat_s2_num_1d7', 'b2g_gender_gof', 'job_f1_addf_num_1d3',
                'geek_chat_s5_num_1d7', 'geek_workdist_sensi', 'b2g_w2v_orig_gof', 'geek_min_active_tdiff',
                'b2g_skill_match', 'exp_f1_pas_view_num_1d7', 'b2g_school_level_gof', 'geek_min_chat_tdiff',
                'exp_det_num_24h', 'geek_desc_position_score', 'b2g_w2v_pref_gof', 'geek_apply_status',
                'b2g_work_distance', 'exp_resp_rate_1d3', 'b2g_apply_status_gof', 'geek_ret_num_1d3',
                'b2g_workyears_gof', 'b2g_degree_rel_match', 'geek_work_memo_position_score', 'exp_list_num_1d3',
                'b2g_static_rel_match', 'b2g_fresh_graduate_gof', 'b2g_degree_gof', 'b2g_pos_workyears_major_match',
                'geek_project_position_score', 'exp_pas_addf_num_4d7', 'b2g_school_type1_gof', 'b2g_salary_gof',
                'exp_f1_pas_addf_rate_1d7', 'b2g_school_type2_gof', 'job_f1_pas_resp_rate_1d3',
                'b2g_title_w2v_orig_gof', 'exp_f1_pas_addf_det_rate_1d7', 'exp_f1_pas_det_list_rate_1d7']

    f_path = "/home/desktop/train_data/boss/"
    dates = ["2019-05-10"]
    fea_name = "boss_l1code,boss_l2code,boss_position,boss_combine_code,boss_city,geek_position,geek_combine_code,boss_id,job_id,geek_id,exp_id,page,rank,list_time,deal_time,lid,pk_class,session,deal_type,detailed_deal_type,geek_degree_new,req_id,job_workyears,job_degree,geek_gender,geek_degree,geek_apply_status,geek_workyears,jl,jh,geek_school_level,geek_cmp_level,b2g_degree_match,b2g_degree_rel_match,b2g_salary_match,b2g_salary_rel_match,b2g_workyears_match,b2g_workyears_rel_match,b2g_static_match,b2g_static_rel_match,job_f1_addf_num_1d3,job_f1_addf_rate_1d3,job_f1_pas_resp_rate_1d3,geek_notify_num_1d3,geek_ret_num_1d3,geek_chat_s2_num_1d7,geek_chat_s5_num_1d7,exp_list_num_1d3,exp_pas_addf_num_1d3,exp_pas_addf_num_4d7,exp_resp_num_1d3,exp_resp_rate_1d3,exp_f1_pas_det_list_rate_1d7,exp_f1_pas_addf_det_rate_1d7,b2g_skill_match,b2g_w2v_pref_gof,b2g_w2v_orig_gof,b2g_pastpos_addf_rate,exp_f1_pas_addf_rate_1d7,b2g_als_gof,b2g_school_level_gof,b2g_school_type1_gof,b2g_school_type2_gof,b2g_fresh_graduate_gof,b2g_degree_gof,b2g_gender_gof,b2g_apply_status_gof,geek_min_active_tdiff,geek_min_chat_tdiff,exp_det_num_24h,exp_pas_addf_num_24h,exp_resp_rate_24h,boss_eh_jl,boss_jh_el,exp_f1_pas_view_num_1d7,b2g_salary_gof,b2g_workyears_gof,b2g_cmp_level_gof,b2g_pos_major_match,b2g_pos_workyears_major_match,b2g_pos_skill_match,b2g_position_similarity,geek_desc_position_score,geek_project_position_score,geek_work_memo_position_score,boss_cmp_level,b2g_overseas_match,job_overseas_tag,geek_overseas_tag,exp_register_tdiff,b2g_title_w2v_orig_gof,b2g_major_w2v_gof,b2g_school_w2v_gof,b2g_jd_workyears_gof,b2g_jd_gender_gof,b2g_jd_degree_gof,b2g_jd_age_gof,b2g_jd_major_gof,b2g_work_distance,geek_workdist_sensi,b2g_company_gof,exp_f1_pas_cp_addf_rate_1d7,geek_simpos_workyears,headhunter_company_addf_num,title_type,b2g_cdssm_gof,b2g_pjenn_gof,exp_min_active_tdiff,b2g_pos_pastpos_similarity,b2g_age_gof,b2g_position_gof,b2g_pos_similarity,b2g_position_addf_rate,g2b_position_addf_rate,b2g_city_addf_rate,geek_city,eh,el,geek_major"
    fea_name = fea_name.split(',')
    fea_index = {}
    for i in range(len(fea_name)):
        fea_index[fea_name[i]] = i

    xgb_train = XgboostTrain(f_path, dates, 30000, 10000, 1, 1, features, m_path)
    xgb_train.train()